// services/chatService.js
import {hostAddr} from '@/config.js';

// 创建聊天
export async function createChat(userId, mode, situation) {
  const url = `${hostAddr}/chat`;
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      user_id: userId,
      mode,
      situation
    })
  });

  if (!response.ok) throw new Error('创建聊天失败');
  return await response.json();
}

// 保存消息
export async function saveMessage(chatId, role, content) {
  const url = `${hostAddr}/chat/${chatId}`;
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({role, content})
  });

  if (!response.ok) throw new Error('保存消息失败');
  return await response.json();
}

// 获取聊天历史
export async function getChatHistory(chatId) {
  const url = `${hostAddr}/chat/${chatId}`;
  const response = await fetch(url, {method: 'GET'});

  if (!response.ok) throw new Error('获取聊天历史失败');
  return await response.json();
}